﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Autenticacao
{
    public partial class frm_exploracao : Form
    {
        System.Data.SqlClient.SqlConnection con;
        
        public Utilizador utie1;
        public Cliente cli3;
        public Exploracao ex1;
        private string idc;
        
        public frm_exploracao(Utilizador vet)
        {
            this.utie1 = vet;
            InitializeComponent();

            this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

        private void pesquisar_cliente()
        {
            con = new System.Data.SqlClient.SqlConnection();
            con.ConnectionString = @"Data Source = ANDRÉ-PC\SQLEXPRESS; Initial Catalog = horsepw_bd; Integrated Security = True;";
            con.Open();

            int id = utie1.get_id_vet();
            SqlCommand command1 = con.CreateCommand();
            command1.CommandText = "SELECT c.nome, id_exploracao, tipo_exploracao, nro_animais, ex.id_cliente, u.id_veterinario FROM Exploracao ex INNER JOIN Cliente c ON ex.id_cliente = c.id_cliente INNER JOIN Utilizador u ON c.id_veterinario = u.id_veterinario WHERE c.nome LIKE '%" + txtge_nomecli.Text + "%' and u.id_veterinario = " + id + " and ex.oculto = 0";
            SqlDataReader readatrib = command1.ExecuteReader();

            this.dataGridView1.Rows.Clear();

            while (readatrib.Read())
            {
                int i = this.dataGridView1.Rows.Add();

                this.dataGridView1[0, i].Value = readatrib["nome"].ToString();
                this.dataGridView1[1, i].Value = readatrib["id_exploracao"].ToString();
                this.dataGridView1[2, i].Value = readatrib["tipo_exploracao"].ToString();
                this.dataGridView1[3, i].Value = readatrib["nro_animais"].ToString();
               
            }

            con.Close();
        }

        private void cmdge_pesquisar_Click(object sender, EventArgs e)
        {
            try
            {
                pesquisar_cliente();
            }
            catch (Exception er)
            {
                MessageBox.Show(er.ToString());
            }
        }

        private void cmdge_insexp_Click(object sender, EventArgs e)
        {
            frm_gexpl1UI frm1 = new frm_gexpl1UI(utie1);
            frm1.ShowDialog();
        }

        private void cmdge_atusel_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.RowCount != 0)
            {
                con = new System.Data.SqlClient.SqlConnection();
                con.ConnectionString = @"Data Source = ANDRÉ-PC\SQLEXPRESS; Initial Catalog = horsepw_bd; Integrated Security = True;";
                con.Open();

                int i = this.dataGridView1.CurrentRow.Index;
                int idv = utie1.get_id_vet();
                string temp_nc = this.dataGridView1.Rows[i].Cells["nome"].Value.ToString();
                SqlCommand command1 = con.CreateCommand();
                command1.CommandText = "SELECT id_cliente, nome, morada, cod_postal, localidade, telemovel, email FROM Cliente WHERE nome = '" + temp_nc + "' and id_veterinario = " + idv + " and oculto = 0";
                SqlDataReader readatrib = command1.ExecuteReader();
                while (readatrib.Read())
                {
                    cli3 = new Cliente(Convert.ToInt32(readatrib["id_cliente"].ToString()), readatrib["nome"].ToString(), readatrib["morada"].ToString(), Convert.ToInt32(readatrib["cod_postal"].ToString()), readatrib["localidade"].ToString(), Convert.ToInt32(readatrib["telemovel"].ToString()), readatrib["email"].ToString());
                }
                int temp_cid = cli3.get_id();
                ex1 = new Exploracao(Convert.ToInt32(dataGridView1["id_exploracao", dataGridView1.CurrentRow.Index].Value.ToString()), dataGridView1["tipo_exploracao", dataGridView1.CurrentRow.Index].Value.ToString(), Convert.ToInt32(dataGridView1["nro_animais", dataGridView1.CurrentRow.Index].Value.ToString()), temp_cid);

                frm_insexploracao frm2 = new frm_insexploracao(cli3, ex1, "editar");
                frm2.ShowDialog();
            }
            else
                MessageBox.Show("Tem de selecionar uma linha!", "Por favor selecione", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

        private void cmdge_delexp_Click(object sender, EventArgs e)
        {
            try
            {
                var op = MessageBox.Show("Tem a certeza que pretende remover a exploração selecionada?", "Confirmar", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                if (op == DialogResult.Yes)
                {
                    if (this.dataGridView1.RowCount != 0)
                    {
                        int i = this.dataGridView1.CurrentRow.Index;
                        con = new System.Data.SqlClient.SqlConnection();
                        con.ConnectionString = @"Data Source = ANDRÉ-PC\SQLEXPRESS; Initial Catalog = horsepw_bd; Integrated Security = True;";
                        con.Open();

                        string temp_nc = this.dataGridView1.Rows[i].Cells["nome"].Value.ToString();
                        string ide = this.dataGridView1.Rows[i].Cells["id_exploracao"].Value.ToString();
                        int idv = utie1.get_id_vet();

                        SqlCommand command98 = con.CreateCommand();
                        command98.CommandText = "SELECT id_cliente, nome, morada, cod_postal, localidade, telemovel, email FROM Cliente WHERE nome = '" + temp_nc + "' and id_veterinario = " + idv + " and oculto = 0";
                        SqlDataReader readatrib98 = command98.ExecuteReader();

                        while (readatrib98.Read())
                        {
                            idc = readatrib98["id_cliente"].ToString();
                        }

                        con.Close();

                        con = new System.Data.SqlClient.SqlConnection();
                        con.ConnectionString = @"Data Source = ANDRÉ-PC\SQLEXPRESS; Initial Catalog = horsepw_bd; Integrated Security = True;";
                        con.Open();
                        //marcar como oculto ao invés de eliminar
                        SqlCommand command99 = con.CreateCommand();
                        command99.CommandText = "Update Exploracao Set oculto = 1 WHERE id_cliente = '" + idc + "' and id_exploracao = '"+ ide +"'";
                        SqlDataReader readatrib99 = command99.ExecuteReader();

                        MessageBox.Show("Exploração eliminada com sucesso!", "Exploração eliminada");
                        con.Close();
                        this.dataGridView1.Rows.Clear();
                    }
                    else
                        MessageBox.Show("Primeiro tem de selecionar uma exploração!");
                }
            }
            catch (Exception er)
            {
                MessageBox.Show(er.ToString());
            }
        }

        private void cmdge_voltar_Click(object sender, EventArgs e)
        {
            frm_gesclinica frm1 = new frm_gesclinica(utie1);
            this.Close();
            frm1.ShowDialog();
        }

        private void cmdge_vacina_Click(object sender, EventArgs e)
        {
            try
            {
                var op = MessageBox.Show("Tem a certeza que pretende inserir um registo de vacinação?", "Confirmar", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                if (op == DialogResult.Yes)
                {
                    if (this.dataGridView1.RowCount != 0)
                    {
                        int i = this.dataGridView1.CurrentRow.Index;

                        con = new System.Data.SqlClient.SqlConnection();
                        con.ConnectionString = @"Data Source = ANDRÉ-PC\SQLEXPRESS; Initial Catalog = horsepw_bd; Integrated Security = True;";
                        con.Open();

                        string ide = this.dataGridView1.Rows[i].Cells["id_exploracao"].Value.ToString();
                        
                        SqlCommand command96 = con.CreateCommand();
                        command96.CommandText = "SELECT id_exploracao, tipo_exploracao, nro_animais, id_cliente FROM Exploracao WHERE id_exploracao = '" + ide + "' and oculto = 0";
                        SqlDataReader readatrib96 = command96.ExecuteReader();

                        while (readatrib96.Read())
                        {
                            ex1 = new Exploracao(Convert.ToInt32(readatrib96["id_exploracao"].ToString()), readatrib96["tipo_exploracao"].ToString(), Convert.ToInt32(readatrib96["nro_animais"].ToString()), Convert.ToInt32(readatrib96["id_cliente"].ToString()));
                        }

                        con.Close();

                        frm_explvaci frm3 = new frm_explvaci(ex1, null, "novo", "aaaa/MM/dd");
                        frm3.ShowDialog();
                    }
                    else
                        MessageBox.Show("Tem de pesquisar primeiro por uma exploração!", "Pesquise a exploração!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception er)
            {
                MessageBox.Show(er.ToString());
            }
        }

        private void cmdge_ac_Click(object sender, EventArgs e)
        {
            try
            {
                var op = MessageBox.Show("Tem a certeza que pretende abrir uma nova consulta?", "Confirmar", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                if (op == DialogResult.Yes)
                {
                    if (this.dataGridView1.RowCount != 0)
                    {
                        int i = this.dataGridView1.CurrentRow.Index;

                        con = new System.Data.SqlClient.SqlConnection();
                        con.ConnectionString = @"Data Source = ANDRÉ-PC\SQLEXPRESS; Initial Catalog = horsepw_bd; Integrated Security = True;";
                        con.Open();

                        string ide = this.dataGridView1.Rows[i].Cells["id_exploracao"].Value.ToString();

                        SqlCommand command96 = con.CreateCommand();
                        command96.CommandText = "SELECT id_exploracao, tipo_exploracao, nro_animais, id_cliente FROM Exploracao WHERE id_exploracao = '" + ide + "' and oculto = 0";
                        SqlDataReader readatrib96 = command96.ExecuteReader();

                        while (readatrib96.Read())
                        {
                            ex1 = new Exploracao(Convert.ToInt32(readatrib96["id_exploracao"].ToString()), readatrib96["tipo_exploracao"].ToString(), Convert.ToInt32(readatrib96["nro_animais"].ToString()), Convert.ToInt32(readatrib96["id_cliente"].ToString()));
                        }

                        con.Close();

                        frme_cons frm4 = new frme_cons(ex1);
                        frm4.ShowDialog();
                    }
                    else
                        MessageBox.Show("Tem de pesquisar primeiro por uma exploração!", "Pesquise a exploração!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception er)
            {
                MessageBox.Show(er.ToString());
            }
        }

        private void cmdge_vc_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.RowCount != 0)
            {
                int i = this.dataGridView1.CurrentRow.Index;

                con = new System.Data.SqlClient.SqlConnection();
                con.ConnectionString = @"Data Source = ANDRÉ-PC\SQLEXPRESS; Initial Catalog = horsepw_bd; Integrated Security = True;";
                con.Open();

                int ide = Convert.ToInt32(this.dataGridView1.Rows[i].Cells["id_exploracao"].Value.ToString());
                string nc = this.dataGridView1.Rows[i].Cells["nome"].Value.ToString();

                frm_consultas frm5 = new frm_consultas(null, nc, "exploracao", ide);
                frm5.ShowDialog();
            } else
                MessageBox.Show("Tem de pesquisar primeiro por uma exploração!", "Pesquise a exploração!", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}
